package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.BuyerBean;
import com.project.bean.BuyerCarBean;
import com.project.bean.SupplierBean;
import com.project.mapper.*;
import com.project.service.IBuyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class BuyerServiceImpl implements IBuyerService {
    @Autowired
    private IBuyerMapper buyerMapper;
    @Autowired
    private IBuyerInfoMapper infoMapper;
    @Autowired
    private IBuyerCarMapper carMapper;
    @Autowired
    private IGoodsMapper goodsMapper;
    @Autowired
    private ISupplierMapper supplierMapper;
    @Override
    public void add(BuyerBean buyerBean, Integer userId) {
           buyerMapper.insert(buyerBean);
        QueryWrapper<BuyerCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
          List<BuyerCarBean> CarBean = carMapper.selectList(qw);
          infoMapper.addInfo(buyerBean.getId(),CarBean);
          goodsMapper.updateBy(CarBean);
            carMapper.delete(qw);
    }

    @Override
    public IPage<BuyerBean> findByItem(Integer pageNo, String name, LocalDate startDate, LocalDate endDate) {

        return buyerMapper.findByItem(new Page(pageNo,5),name,startDate,endDate);

    }

    @Override
    public BuyerBean findById(Integer id) {
        BuyerBean buyerBean = buyerMapper.selectById(id);
        buyerBean.setBuyerBean(infoMapper.findList(id));
        buyerBean.setSupplierBean(supplierMapper.findByBuyerId(id));
        return buyerBean;
    }
}
